﻿#include "mainwin.h"
#include "ui_mainwin.h"

mainWin::mainWin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainWin)
{
    view=new QWebEngineView(parent);
    view->load(QUrl("file://./remain/error.html"));
    view->move(QApplication::desktop()->width()*0.75,0);
    view->resize(QApplication::desktop()->width()*0.25,QApplication::desktop()->height());
    ui->setupUi(this);
    ui->p_1->setShortcut(tr("up"));
    ui->p_s->setShortcut(tr("down"));
    ui->p_3->setShortcut(tr("left"));
    ui->p_5->setShortcut(tr("right"));

    /************
     * 信号和槽连接
     * *********/
    setWindowTitle(QString::fromLocal8Bit("iMakeit校园巡逻机器人机器人控制程序"));
    leftColor=new QColorDialog;
    rightColor=new QColorDialog;
    bothColor=new QColorDialog;
    //设置ip按钮
    QObject::connect(ui->p_ip,&QPushButton::clicked,this,[=](void){
        host=QHostAddress(ui->ip_line->text());
        qDebug()<<host;
    });
    //设置端口
    QObject::connect(ui->p_port,&QPushButton::clicked,this,[=](void){
        port=ui->port_line->text().toInt();
        qDebug()<<port;
    });
    //发送命令
    QObject::connect(ui->p_send,&QPushButton::clicked,this,[=](void){
        sendCmd(ui->cmd_line->text());
       // qDebug()<<ui->cmd_line->text().toUInt(ok,16);
    });
    //获取录制保持路径
    QObject::connect(ui->p_photo,&QPushButton::clicked,this,[=](void){
        saveFile=QFileDialog::getOpenFileName(this);
        file->setFileName(saveFile);
        file->open(QIODevice::WriteOnly|QIODevice::Text);
        onPhoto=1;
        timer->start();
        qDebug()<<saveFile;
    });
    //停止录制
    QObject::connect(ui->p_stop_photo,&QPushButton::clicked,this,[=](void){
        saveFile="";
        onPhoto=0;
        b_timeStr = "";
        file->write("STOP_IDENTIFY");
        file->close();
       qDebug()<<"stop";
       QMessageBox::information(this,QString::fromLocal8Bit("录制完成"),QString::fromLocal8Bit("录制完成"),
                                QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    });
    //回放
    QObject::connect(ui->p_back,&QPushButton::clicked,this,[=](void){
       saveFile=QFileDialog::getOpenFileName(this);
       file->setFileName(saveFile);
       file->open(QIODevice::ReadOnly|QIODevice::Text);
       file->readLine();
       QString swap;
       while(1)
       {
           swap=file->readLine();
           if(swap=="STOP_IDENTIFY")
               break;
           sendCmd(swap);
           swap=file->readLine();
           if(swap=="STOP_IDENTIFY")
               break;
           Delay_MSec(swap.toInt());

       }
       QMessageBox::information(this,QString::fromLocal8Bit("回放完成"),QString::fromLocal8Bit("回放完成"),
                                QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
       file->close();
       qDebug()<<"onbacking";
    });
    QObject::connect(ui->p_1,&QPushButton::clicked,this,[=](void){
        sendCmd("F5FFFFFF");
        qDebug()<<"前";
    });
    QObject::connect(ui->p_2,&QPushButton::clicked,this,[=](void){
        //sendCmd("F3FFFFFF");
        qDebug()<<"后";//12.20
    });
    QObject::connect(ui->p_3,&QPushButton::clicked,this,[=](void){
        sendCmd("F1FFFFFF");
        qDebug()<<"左";
    });
    QObject::connect(ui->p_5,&QPushButton::clicked,this,[=](void){
        sendCmd("F4FFFFFF");
        qDebug()<<"右";
    });
    QObject::connect(ui->p_s,&QPushButton::clicked,this,[=](void){
        sendCmd("FE111111");
        qDebug()<<"停";
    });
    //mp3播放() 重写
    QObject::connect(ui->p_on_mp3,&QPushButton::clicked,this,[=](void){
        char str[9];
        uint sum= ui->s_mp3->text().toUInt();
        int i_s1,i_s2;
        i_s1=(sum/100)+1;
        i_s2=(sum%100)+1;
        QString s1=itoa(i_s1,str,16);
        QString s2=itoa(i_s2,str,16);
        if(s1.length()==1)
        {
            s1="0"+s1;
        }
        if(s2.length()==1)
        {
            s2="0"+s2;
        }
        sendCmd(QString("DE")+s1+s2+"FF");
        qDebug()<<QString("DE")+s1+s2+"FF";
        qDebug()<<"播放";
    });
    //重置机器
    QObject::connect(ui->p_reset,&QPushButton::clicked,this,[=](void){
        sendCmd("11FFFFFF");
        qDebug()<<QString::fromLocal8Bit("重置机器");
    });
    //调控MP3音量
    QObject::connect(ui->MP3_volume,&QSlider::valueChanged,this,[=](void){
        char str[9];
        QString s=itoa(ui->MP3_volume->value(),str,16);
        if(s=="0")
        {
            s="FF";
        }
        else if(s.length()==1){
            s="0"+s;
        }
        qDebug()<<QString::fromLocal8Bit("音量:")<<s;
        sendCmd("DF"+s+"FFFF");
    });
    //关闭MP3
    QObject::connect(ui->p_cut_mp3,&QPushButton::clicked,this,[=](void){
       qDebug()<<QString::fromLocal8Bit("关闭MP3");
        sendCmd("DD01FFFF");
    });
    //MP3暂停
    QObject::connect(ui->p_stop_mp3,&QPushButton::clicked,this,[=](void){
       qDebug()<<QString::fromLocal8Bit("暂停MP3");
        sendCmd("DD05FFFF");
    });
    //单曲循环
    QObject::connect(ui->p_always_on_mp3,&QPushButton::clicked,this,[=](void){
       qDebug()<<QString::fromLocal8Bit("循环播放MP3");
        sendCmd("DD03FFFF");
    });
    //舵机控制
    QObject::connect(ui->left_eye,&QSlider::valueChanged,this,[=](void){
       qDebug()<<QString::fromLocal8Bit("舵机控制");
       QString s;
       char str[9];
       s=itoa(ui->left_eye->value(),str,16);
       if(s=="0")
       {
           s="FF";
       }
       else if(s.length()==1){
           s="0"+s;
       }
       qDebug()<<s;
       sendCmd("EF"+s+"FFFF");
    });
    QObject::connect(ui->right_eye,&QSlider::valueChanged,this,[=](void){
       qDebug()<<QString::fromLocal8Bit("舵机控制");
       QString s;
       char str[9];
       s=itoa(ui->right_eye->value(),str,16);

       if(s.length()==1){
           s="0"+s;
       }
       qDebug()<<s;
       sendCmd("EE"+s+"FFFF");
    });
    //更改RGB灯盘颜色
    QObject::connect(ui->p_change_left_eye_color,&QPushButton::clicked,this,[=](void){
    qDebug()<<QString::fromLocal8Bit("更改左眼颜色");
    leftColor->exec();
    });
    QObject::connect(ui->p_change_right_eye_color,&QPushButton::clicked,this,[=](void){
    qDebug()<<QString::fromLocal8Bit("更改右眼颜色");
    rightColor->exec();//backRightColor,this,QString::fromLocal8Bit("选择你要的颜色")
    });
    QObject::connect(ui->p_change_left_and_right_eye_color,&QPushButton::clicked,this,[=](void){
    qDebug()<<QString::fromLocal8Bit("更改双眼颜色");
    bothColor->exec();
    });
    //生气
    QObject::connect(ui->p_angry,&QPushButton::clicked,this,[=](void){
        sendCmd("c2FEFFFF");
        sendCmd("c1FEFFFF");
    });
    //平静
    QObject::connect(ui->p_peace,&QPushButton::clicked,this,[=](void){
        sendCmd("c2FFFEFF");
        sendCmd("c1FFFEFF");
    });
    //开心
    QObject::connect(ui->p_happy,&QPushButton::clicked,this,[=](void){
        sendCmd("c2C20CC8");
        sendCmd("c1C20CC8");
    });
    QObject::connect(ui->check_real_time_color,&QCheckBox::stateChanged,this,[=](int state){

        if (state == Qt::Checked) // "选中"
        {
            boolCheckRealTimeColor=1;
            qDebug()<<"checked"<<boolCheckRealTimeColor;
        }
        else // 未选中 - Qt::Unchecked
        {
            boolCheckRealTimeColor=0;
            qDebug()<<"unchecked"<<boolCheckRealTimeColor;
        }
    });
    QObject::connect(leftColor,&QColorDialog::currentColorChanged,this,[=](void){
        if(boolCheckRealTimeColor)
        {

            char str[9];
            QColor color = leftColor->currentColor();
            if(!color.isValid()) return;
            //qDebug()<<color;
            QString r,g,b;
            r=itoa(color.red()-1,str,16);
            g=itoa(color.green()-1,str,16);
            b=itoa(color.blue()-1,str,16);
            if (r=="ffffffff") r="FF";
            if (g=="ffffffff") g="FF";
            if (b=="ffffffff") b="FF";
            qDebug()<<r<<g<<b;
            sendCmd("c1"+r+g+b);
        }
    });
    QObject::connect(leftColor,&QColorDialog::colorSelected,this,[=](void){
            char str[9];
            QColor color = leftColor->currentColor();
            if(!color.isValid()) return;
            //qDebug()<<color;
            QString r,g,b;
            r=itoa(color.red()-1,str,16);
            g=itoa(color.green()-1,str,16);
            b=itoa(color.blue()-1,str,16);
            if (r=="ffffffff") r="FF";
            else if(r.length()==1) r="0"+r;
            if (g=="ffffffff") g="FF";
            else if(g.length()==1) g="0"+g;
            if (b=="ffffffff") b="FF";
            else if(b.length()==1) b="0"+b;
            qDebug()<<r<<g<<b;
            sendCmd("c1"+r+g+b);
    });
    QObject::connect(rightColor,&QColorDialog::currentColorChanged,this,[=](void){
        qDebug()<<"?";
        if(boolCheckRealTimeColor)
        {
            char str[9];
            QColor color = rightColor->currentColor();
            if(!color.isValid()) return;
            //qDebug()<<color;
            QString r,g,b;
            r=itoa(color.red()-1,str,16);
            g=itoa(color.green()-1,str,16);
            b=itoa(color.blue()-1,str,16);
            if (r=="ffffffff") r="FF";
            if (g=="ffffffff") g="FF";
            if (b=="ffffffff") b="FF";
            qDebug()<<r<<g<<b;
            sendCmd("c2"+r+g+b);
        }
    });
    QObject::connect(rightColor,&QColorDialog::colorSelected,this,[=](void){
            char str[9];
            QColor color = rightColor->currentColor();
            if(!color.isValid()) return;
            //qDebug()<<color;
            QString r,g,b;
            r=itoa(color.red()-1,str,16);
            g=itoa(color.green()-1,str,16);
            b=itoa(color.blue()-1,str,16);
            if (r=="ffffffff") r="FF";
            else if(r.length()==1) r="0"+r;
            if (g=="ffffffff") g="FF";
            else if(g.length()==1) g="0"+g;
            if (b=="ffffffff") b="FF";
            else if(b.length()==1) b="0"+b;
            qDebug()<<r<<g<<b;
            sendCmd("c2"+r+g+b);
    });
    QObject::connect(bothColor,&QColorDialog::currentColorChanged,this,[=](void){
        if(boolCheckRealTimeColor)
        {

            char str[9];
            QColor color = bothColor->currentColor();
            if(!color.isValid()) return;
            //qDebug()<<color;
            QString r,g,b;
            r=itoa(color.red()-1,str,16);
            g=itoa(color.green()-1,str,16);
            b=itoa(color.blue()-1,str,16);
            if (r=="ffffffff") r="FF";
            if (g=="ffffffff") g="FF";
            if (b=="ffffffff") b="FF";
            qDebug()<<r<<g<<b;
            sendCmd("c1"+r+g+b);
            sendCmd("c2"+r+g+b);
        }
    });
    QObject::connect(bothColor,&QColorDialog::colorSelected,this,[=](void){
            char str[9];
            QColor color = bothColor->currentColor();
            if(!color.isValid()) return;
            //qDebug()<<color;
            QString r,g,b;
            r=itoa(color.red()-1,str,16);
            g=itoa(color.green()-1,str,16);
            b=itoa(color.blue()-1,str,16);
            if (r=="ffffffff") r="FF";
            else if(r.length()==1) r="0"+r;
            if (g=="ffffffff") g="FF";
            else if(g.length()==1) g="0"+g;
            if (b=="ffffffff") b="FF";
            else if(b.length()==1) b="0"+b;
            qDebug()<<r<<g<<b;
            sendCmd("c1"+r+g+b);
            sendCmd("c2"+r+g+b);

    });
    connect(ui->p_start_esp32,&QPushButton::clicked,this,[=](){
        esp32host=QHostAddress(ui->ip_line_esp32->text());
        if(esp32host==QHostAddress("")) return;
        qDebug()<<"esp32!";
        hide();
        move(0,0);
        resize(QApplication::desktop()->width()*0.75,QApplication::desktop()->height());
        show();
        view->page()->load(QUrl("http://"+ui->ip_line_esp32->text()));
        view->show();

    });
    int i=0;
    connect(mSocket,&QUdpSocket::readyRead,this,[=,&i](){
        qDebug()<<"!!!";
        QByteArray arr;
           arr.resize(mSocket->bytesAvailable());
           mSocket->readDatagram(arr.data(),arr.size());
           qDebug()<<arr;
           QImage img;
           img.fromData(arr);
           ui->label_c->setPixmap(QPixmap::fromImage(img));
          // ui->label_c->setText(arr);
           mSocket->writeDatagram((QString("next one")+i).toStdString().c_str(),esp32host,3333);

            i++;

    });
}
mainWin::~mainWin()
{
    delete ui;
}
